﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Research.BLL;
using Research.Model;

namespace Research.Web
{
    public partial class ShowCharts : System.Web.UI.Page
    {
        readonly BasicBll _bllBasic = new BasicBll();
        readonly RprjMainBll _bllRprjMain=new RprjMainBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridPlanSchedule();

                btnScheduleUpdate.OnClientClick = gridPlanSchedule.GetNoSelectionAlertInTopReference("没有选中项!");
                btnScheduleDel.OnClientClick = gridPlanSchedule.GetNoSelectionAlertInTopReference("没有选中项!");
                btnAdd.OnClientClick = gridPlanSchedule.GetNoSelectionAlertInTopReference("没有选中项!");
                btnDel.OnClientClick = gridActualSchedule.GetNoSelectionAlertInTopReference("没有选中项!");

                btnScheduleAdd.OnClientClick +=
                    windowPlanSchedule.GetShowReference(
                        "RprjScheduleHandle.aspx?RprjID=" + Request["rPrjID"] + "&Type=Add", "计划进度添加");

            }
        }

        protected void BindGridPlanSchedule()
        {
            gridPlanSchedule.DataSource = _bllBasic.GetPlanScheduleList(Convert.ToInt32(Request["rPrjID"]));
            gridPlanSchedule.DataBind();
        }

        protected void BindGridActualSchedule(MRprjPlanSchedule mPlanSchedule)
        {
            gridActualSchedule.DataSource = _bllBasic.GetActualScheduleList(mPlanSchedule);
            gridActualSchedule.DataBind();
        }

        protected void gridPlanSchedule_RowClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            var mPlanSchedule = new MRprjPlanSchedule
                                                  {
                                                      RprjID = Convert.ToInt32(Request["rPrjID"]),
                                                      AutoNum =
                                                          Convert.ToInt32(gridPlanSchedule.Rows[e.RowIndex].DataKeys[0])
                                                  };

            ViewState["MRprjPlanSchedule"] = mPlanSchedule;
            BindGridActualSchedule(mPlanSchedule);

            btnScheduleUpdate.OnClientClick += windowPlanSchedule.GetShowReference("RprjScheduleHandle.aspx?RprjID=" + Request["rPrjID"] + "&AutoNum=" + gridPlanSchedule.Rows[e.RowIndex].DataKeys[0] + "&Type=Update", "计划进度修改");
       }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var mPlanSchedule = (MRprjPlanSchedule) ViewState["MRprjPlanSchedule"];

            _bllRprjMain.InsertRprjActualSchedule(new MRprjActualSchedule
                                                      {
                                                          RprjID = mPlanSchedule.RprjID,
                                                          AutoNum = mPlanSchedule.AutoNum,
                                                          Percentage = Convert.ToDouble(nbxPercentage.Text),
                                                          RegDate = DateTime.Now
                                                      });
            BindGridActualSchedule(mPlanSchedule);
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            var index = gridActualSchedule.SelectedRowIndexArray[0];
            _bllRprjMain.DelRprjActualSchedule(new MRprjActualSchedule
                                                   {
                                                       RprjID = Convert.ToInt32(gridActualSchedule.Rows[index].DataKeys[0]),
                                                       AutoNum = Convert.ToInt32(gridActualSchedule.Rows[index].DataKeys[1]),
                                                       RegDate = (DateTime)gridActualSchedule.Rows[index].DataKeys[2]
                                                   });
            var mPlanSchedule = (MRprjPlanSchedule)ViewState["MRprjPlanSchedule"];
            ViewState["MRprjPlanSchedule"] = mPlanSchedule;
            BindGridActualSchedule(mPlanSchedule);
        }

        protected void btnScheduleDel_Click(object sender, EventArgs e)
        {
            var index = gridPlanSchedule.SelectedRowIndexArray[0];
            if (!_bllRprjMain.DelRprjPlanSchedule(new MRprjPlanSchedule
                                                  {
                                                      RprjID = Convert.ToInt32(Request["rPrjID"]),
                                                      AutoNum =
                                                           Convert.ToInt32(gridPlanSchedule.Rows[index].DataKeys[0])
                                                  }))

                ExtAspNet.Alert.Show("该进度已有完成率，不可直接删除！");

        }

        protected void windowPlanSchedule_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            Response.Redirect(string.Format("ShowCharts.aspx?Title={0}&rPrjID={1}", Request["Title"], Request["rPrjID"]));
        }
    }
}